package com.lngsyz.ywtb.util;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.ResourceBundle;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;

public class StringUtil {
	
	// 编码方式
    private static final String ENCODE_UTF = "UTF-8";
    
    /**
	 * 获取资源文件
	 * 
	 * @param propertiesFile
	 * @param itemIndex
	 * @return
	 */
	public static String getConfiguration(String propertiesFile, String itemIndex) {
		try {
			Configuration configs = new PropertiesConfiguration(propertiesFile);
			return configs.getString(itemIndex);
		} catch (Exception e) {
			return "";
		}
	}
    
    /**
     * number
     * @param str
     * @return
     */
	public static boolean isNumeric(String str) {
		if(StringUtils.isNotBlank(str)) {
			for (int i = str.length(); --i >= 0;) {
				if (!Character.isDigit(str.charAt(i))) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
    
	/**
     * 计算中英文字符串的字节长度 <br/>
     * 一个中文占3个字节
     *
     * @param str
     * @return int 字符串的字节长度
     */
    public static int getLength(String str) {
        if (str == null || str.length() == 0) {
            return 0;
        }
        try {
            return str.getBytes(ENCODE_UTF).length;
        } catch (UnsupportedEncodingException e) {
        	e.printStackTrace();
        }
        return 0;
    }

	public static String createUuid() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	public static String getConfig(String propertiesFile, String itemIndex) {
		String configStr = "";
		try {
			configStr = ResourceBundle.getBundle(propertiesFile).getString(itemIndex);
		} catch (Exception e) {
		}
		return configStr;
	}
	
	public static boolean isCorrectPhone(String phone) {
		boolean flag = false;
		if(isMobile(phone) || isPhone(phone)) {
			flag = true;
		}
		return flag;
	}
	
	public static String ListtoString(List list) {
		String s = org.apache.commons.lang.StringUtils.join(list.toArray(), ",");
		return s;
	}

	/**
     * 手机号验证
     * 
     * @param  str
     * @return 验证通过返回true
     */
    private static boolean isMobile(String str) { 
        Pattern p = null;
        Matcher m = null;
        boolean b = false; 
        p = Pattern.compile("^[1][3,4,5,6,7,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches(); 
        return b;
    }
    /**
     * 电话号码验证
     * 
     * @param  str
     * @return 验证通过返回true
     */
    private static boolean isPhone(String str) { 
        Pattern p1 = null,p2 = null;
        Matcher m = null;
        boolean b = false;  
        p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 验证带区号的
        p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的
        if(str.length() >9)
        {   m = p1.matcher(str);
           b = m.matches();  
        }else{
            m = p2.matcher(str);
           b = m.matches(); 
        }  
        return b;
    }
    
    public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
